package cn.ekuma.pos.toolcommand.base;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.jwizardcomponent.FinishAction;
import net.sourceforge.jwizardcomponent.JWizardComponents;
import net.sourceforge.jwizardcomponent.Utilities;
import net.sourceforge.jwizardcomponent.dialog.SimpleJWizardDialog;
import net.xeoh.plugins.base.annotations.PluginImplementation;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.toolcommand.AbstractCommand;
import cn.ekuma.impexp.ImpExpManager;
import cn.ekuma.impexp.model.AbstractImpExpTableDefinition;
import cn.ekuma.impexp.model.transform.ImportColumnMatchTableTransform;
import cn.ekuma.impexp.ui.swing.ColumnMatch;
import cn.ekuma.impexp.ui.swing.Content;
import cn.ekuma.impexp.ui.swing.JMatchTableColumnWizardPanel;
import cn.ekuma.impexp.ui.swing.JSelectTableImportWizardPanel;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.gui.muleSelecter.MuleComboBoxValEntry;
import com.openbravo.data.loader.TableDefinition;

@PluginImplementation
public class ADVTableDataImportCommand extends AbstractCommand {

	@Override
	public void call() throws BasicException {
		final SimpleJWizardDialog importDialog=new SimpleJWizardDialog();
		final JWizardComponents wizardComponents=importDialog.getWizardComponents();
		List<TableDefinition> defTables=((I_DataLogicERP) dataLogic).getImpExpTableList(I_DataLogicERP.TABLE_All);
		   ArrayList<MuleComboBoxValEntry> entrys=new ArrayList<MuleComboBoxValEntry>();
		   for(TableDefinition table:defTables)
			   entrys.add(new MuleComboBoxValEntry(table,table.getTableName()));
		wizardComponents.addWizardPanel(new JSelectTableImportWizardPanel(wizardComponents,entrys));
		final JMatchTableColumnWizardPanel matchWizardPanel=new JMatchTableColumnWizardPanel(wizardComponents);
		wizardComponents.addWizardPanel(matchWizardPanel);
		importDialog.setBounds(0, 0, 700, 600);
		Utilities.centerComponentOnScreen(importDialog);
		wizardComponents.setFinishAction(new FinishAction(wizardComponents){
			@Override
			public void performAction() {
				ArrayList<ColumnMatch> columnMatch=matchWizardPanel.getSelectedMatch();
				AbstractImpExpTableDefinition impExpTable=(AbstractImpExpTableDefinition)wizardComponents.getMap().get(Content.IMPORT_TABLE);
				try {
					ImpExpManager.INSTANCE.doImportToDb(new ImportColumnMatchTableTransform(impExpTable,columnMatch));
					new MessageInf(MessageInf.SGN_SUCCESS,"执行成功").show(importDialog);
				} catch (BasicException e) {
					e.printStackTrace();
					new MessageInf(e).show(importDialog);
				}
			}
		});
		importDialog.setModal(true);
		importDialog.showDialog();
	}

	@Override
	public String getCommandName() {
		return "高级自定义导入";
	}

	@Override
	public String getCommandCategoryName() {
		return "系统";
	}
	
}
